package com.ys.controller.web.kefu;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.kefu.TpGaugeMaintenance;
import com.ys.entity.sys.YsCol;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.R;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/kefu/tpGaugeMaintenance")
public class WebTpGaugeMaintenanceController {

	@Autowired
	private CommonApiService<TpGaugeMaintenance> commonService;
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model) throws Exception{

		return commonService.toAdd(model, new TpGaugeMaintenance(), MenuId.TP_GaugeMaintenance, null, null);
	}

	/*
	 * 修改
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, Long id) throws Exception{

		return commonService.toEdit(model, id, new TpGaugeMaintenance(), MenuId.TP_GaugeMaintenance, null, null);
	}
	//导出
	@RequestMapping("/exportTpGaugeMaintenance")
	@ResponseBody
	public R exportTpGaugeMaintenance(@ModelAttribute TpGaugeMaintenance cond, String conditions) throws Exception {

		cond.setPage(0).setSize(999999);
		cond = setSeachSql(cond);
		cond.setOrderby("A.CreateTime DESC");//排序，新添加在前
		//获取数据
		List<Map<String, Object>> listData = cond.queryMap();
		List<YsCol> listField = new YsCol()
				.setMenuId(MenuId.TP_GaugeMaintenance)
				.where("A.Code != 'ope'")
				.queryList();
		listField.add(new YsCol().setName("制单时间").setCode("createTime"));

		ExportExcelUtils<TpGaugeMaintenance> util = new ExportExcelUtils<>(new TpGaugeMaintenance());

		String toFilePath = util.exportExcel(listData, listField, "箱规维护");
		UploadService.UploadDto dto =commonService.uploadLocalToServer(toFilePath);
		return R.ok(dto.getUrl());
	}
	/**
	 * 设置查询的cond的sql设置
	 * @param cond
	 * @return
	 */
	public TpGaugeMaintenance setSeachSql(TpGaugeMaintenance cond){
		cond.addField("B.code code,B.name name");
		cond.addLeftJoin("TP_ProductActive B ON B.code = A.code ");

		return cond;
	}
	/**
	 * 修改、详情
	 */
	@GetMapping("/toGaugeMaintenance")
	public String toGaugeMaintenance(Model model,Long id,int type){

		Map<String, Object> entity = new TpGaugeMaintenance().setId(id)
				.addField("B.code code,B.name name")
				.addLeftJoin("TP_ProductActive B ON B.code = A.code ")
				.queryFirstMap();
		String code=entity.get("code").toString();
		String name=entity.get("name").toString();
		String warehouseAddress=entity.get("warehouseAddress").toString();
		String num=entity.get("num").toString();
		String length=entity.get("length").toString();
		String height=entity.get("height").toString();
		String width=entity.get("width").toString();
		String weight=entity.get("weight").toString();
		
		WebTpGaugeMaintenanceController.ToEditDto dto = new WebTpGaugeMaintenanceController.ToEditDto(
				code,name,warehouseAddress,
				num,length,height,width,weight
		);

		model.addAttribute("dto", dto);
		if(type==1){//修改
			return "kefu/tpGaugeMaintenance/toEdit";
		}else {//详情
			return "kefu/tpGaugeMaintenance/toDetail";
		}

	}
	@Data
	@AllArgsConstructor
	private static class ToEditDto{
		
		private String code;
		private String name;
		private String warehouseAddress;
		private String num;
		private String length;
		private String height;
		private String width;
		private String weight;
		

	}

	/**
	 * 详情页面
	 */
	@GetMapping("/toDetail")
	public String toDetail(Model model,long Id){

		ToDetailDto dto = new ToDetailDto(Id);

		model.addAttribute("dto", dto);
		return "kefu/tpGaugeMaintenance/toDetail";
	}
	@Data
	@AllArgsConstructor
	private static class ToDetailDto{
		//private JSONArray colArray;
		private long Id;
	}

}
